package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.RpcUserInfoVO;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.ExpressionUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelVariableVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowParticipantInfoVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowCandidateParticipantService;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowModelVariableRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowParticipantRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.UserRepository;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.ProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;



/**
 * @author liyang
 * @version 1.0
 * @date 2021-07-30 09:03:28
 **/
@Service
public class FlowCandidateParticipantServiceImpl implements FlowCandidateParticipantService {

    private transient final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public void saveParticipant(String processDefinitionId, String expression, String value) {
        RepositoryService repositoryService = SpringUtils.getBean(RepositoryService.class);
        FlowModelVariableRepository flowModelVariableRepository = SpringUtils.getBean(FlowModelVariableRepository.class);
        FlowParticipantRepository flowParticipantRepository = SpringUtils.getBean(FlowParticipantRepository.class);
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(expression)) {
            ProcessDefinition processDefinition = repositoryService.getProcessDefinition(processDefinitionId);
            String deploymentId = processDefinition.getDeploymentId();
            String expressionType = "STA";
            List<FlowModelVariableVo> modelVariables = flowModelVariableRepository.getModelVariables(deploymentId);
            for (FlowModelVariableVo flowModelVariable : modelVariables) {
                if (expression.equals(flowModelVariable.getCode())) {
                    expressionType = flowModelVariable.getType();
                }
            }
            FlowParticipantInfoVo flowParticipantInfo = new FlowParticipantInfoVo();
            flowParticipantInfo.setParticipantId(value);
            flowParticipantInfo.setParticipantType(expressionType);
            flowParticipantInfo.setParticipantName(resolveParticipantName(value, ExpressionUtils.trimExpression(expression),expressionType));
            flowParticipantInfo.setCreateTime(new Date());
            flowParticipantInfo.setUpdateTime(new Date());
            flowParticipantRepository.insertParticipant(flowParticipantInfo);
        }
    }

    @Override
    public String resolveParticipantName(String participantId, String expression,String participantType) {
        try {
            if (Objects.equals(participantType,"STA")){
                SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
                selectUserInfoReqVO.setUserId(Long.parseLong(participantId));
                BaseResult<RpcUserInfoVO> result = userFeignService.qryUserInfo(selectUserInfoReqVO);
                RpcUserInfoVO userInfoVO = result.getData();
                return Objects.nonNull(userInfoVO) ? userInfoVO.getUserName() : participantId;
            }else if (Objects.equals(participantType,"ORG")){
                //String orgName = userRepository.getOrgNameByCode(participantId);
                //return Objects.nonNull(orgName) ? orgName : defaultValue;
                return  participantId;
            }
        } catch (Exception e) {
            logger.error("获取参与人名称异常",e);
            return participantId;
        }
        return participantId;
    }
}
